package com.djx.business.access.controller;

import com.djx.business.access.service.UserService;
import com.djx.commons.constants.Constants;
import com.djx.commons.error.error.CommonException;
import com.djx.commons.response.ProfileResult;
import com.djx.commons.response.Result;
import com.djx.commons.response.ResultCode;
import com.djx.entity.access.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.Map;

@RequestMapping("/sys")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result userLogin(@RequestBody Map<String, Object> map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        try {
            //加密
            password = new Md5Hash(password, username, 3).toString();
            Subject subject = SecurityUtils.getSubject();
            User user = userService.findByUsername(username);
            if (user != null) {
                if (user.getUserStatus().equals(Constants.disable)) {
                    return new Result(ResultCode.USER_DISABLE);
                }
                User byUsernameAndPassword = userService.findByUsernameAndPassword(username, password);
                if (byUsernameAndPassword != null) {
                    UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
                    subject.login(usernamePasswordToken);
                    //把当前用户登录后生成的seesionid发送给前台
                    Serializable sessionId = subject.getSession().getId();
                    return new Result(ResultCode.LOGIN_SUCCESS, sessionId);
                }
                return new Result(ResultCode.LOGIN_FAIL_NO_PASSWORD);
            }
            return new Result(ResultCode.LOGIN_FAIL_NO_USER);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.CONTACTANADMINISTRATOR);

        }
    }


    /*  不好使
        try

        {
            subject.login(token);
            System.out.println("登录成功1");
        }catch(
        IncorrectCredentialsException e)

        {
            System.out.println("密码不正确");
        }catch(
        UnknownAccountException e1)

        {
            System.out.println("账号不存在");
        }

    */
//用户成功登录之后,把当前用户的安全数据传递给前台
    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    public Result profile() throws CommonException {
        try {

            //获得主体
            Subject subject = SecurityUtils.getSubject();
            //获得安全数据集合
            PrincipalCollection principals = subject.getPrincipals();

            ProfileResult result = (ProfileResult) principals.getPrimaryPrincipal();

            return new Result(ResultCode.SUCCESS, result);

        } catch (Exception e) {
            throw new CommonException(ResultCode.FAIL);
        }
    }


}
